package com;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import com.example.TukelaExampleDemo.R;

import java.util.List;

public class ViewPagerAdapter extends PagerAdapter{

	//界面列表
	private List<View> views;
	private Activity activity;
	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	public ViewPagerAdapter(List<View> vs, Activity ac) {
		// TODO Auto-generated constructor stub
		this.views = vs;
		this.activity = ac;
	}



	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(views != null)
		{
			return views.size();
		}
		return 0;
	}
	//销毁arg1位置的界面
	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		((ViewPager) container).removeView(views.get(position));
	}

	@Override
	public void finishUpdate(View container) {
		// TODO Auto-generated method stub
		super.finishUpdate(container);
	}

	//初始化position位置的界面
	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub

		((ViewPager) container).addView(views.get(position), 0);
		if(position == views.size()-1)
		{

			Button go_home = (Button) container.findViewById(R.id.go_home);
			go_home.setVisibility(View.VISIBLE);
			go_home.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					//设置已经引导
					setGuide();
					geHome();

				}
			});
		}
		return views.get(position);

	}

	protected void geHome() {
		// TODO Auto-generated method stub
		//跳转
		Intent intent = new Intent(activity, MainActivity.class);
		activity.startActivity(intent);
		activity.finish();
	}


	//设置已经引导过了，下次启动不用再次引导
	protected void setGuide() {
		// TODO Auto-generated method stub
		SharedPreferences preferences = activity.getSharedPreferences(
				SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);

		Editor editor = preferences.edit();
		//存入数据
		editor.putBoolean("isFirstIn", false);
		//提交修改
		editor.commit();
	}

	// 判断是否由对象生成界面  
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return (arg0 == arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

}
